Start here to begin with Stingray.
In [1]:
import numpy as np
%matplotlib inline
In [2]:
from stingray import Lightcurve
A Lightcurve
object can be created in two ways :
Create 1000 time stamps
In [5]:
times = np.arange(1000)
times[:10]
Out[5]:
Create 1000 random count values between 0 to 100
In [6]:
counts = np.random.rand(1000)*100
counts[:10]
Out[6]:
Create a Lightcurve object with the times and counts array.
In [7]:
lc = Lightcurve(times, counts)
The number of data points can be counted with the len
function.
In [8]:
len(lc)
Out[8]:
In [10]:
arrivals = np.loadtxt("photon_arrivals.txt")
arrivals[:10]
Out[10]:
In [11]:
lc_new = Lightcurve.make_lightcurve(arrivals, 1)
The time bins and respective counts can be seen with lc.counts
and lc.time
In [12]:
lc_new.counts
Out[12]:
In [13]:
lc_new.time
Out[13]:
A Lightcurve object has the following properties :
time
: numpy array of time valuescounts
: numpy array of counts per bin valuescountrate
: numpy array of counts per second valuesn
: Number of data points in the lightcurvedt
: Time resolution of the light curvetseg
: Total duration of the light curvetstart
: Start time of the light curve
In [14]:
lc.n == len(lc)
Out[14]:
Two light curves can be summed up or subtracted from each other if they have same time arrays.
In [15]:
lc_rand = Lightcurve(np.arange(1000), [500]*1000)
In [16]:
lc_sum = lc + lc_rand
In [14]:
# We can see the difference between the maximum and minimum values
print(max(lc_sum) - max(lc))
print(min(lc_sum) - min(lc))
A negation operation on the lightcurve object inverts the count array from positive to negative values.
In [17]:
lc_neg = -lc
In [18]:
lc_sum = lc + lc_neg
In [19]:
np.all(lc_sum.counts == 0) # All the points on lc and lc_neg cancel each other
Out[19]:
Count value at a particular time can be obtained using indexing.
In [20]:
lc[120]
Out[20]:
A Lightcurve can also be sliced to generate a new object.
In [21]:
lc_sliced = lc[100:200]
In [22]:
len(lc_sliced.counts)
Out[22]:
Two light curves can be combined into a single object using the join
method. Note that both of them must not have overlapping time arrays.
In [23]:
lc_1 = lc
In [24]:
lc_2 = Lightcurve(np.arange(1000, 2000), np.random.rand(1000)*1000)
In [25]:
lc_long = lc_1.join(lc_2) # Or vice-versa
In [26]:
print(len(lc_long))
A light curve can also be truncated.
In [27]:
lc_cut = lc_long.truncate(start=0, stop=1000)
In [28]:
len(lc_cut)
Out[28]:
Note : The start
and stop
values can also be given as time values.
In [29]:
lc_cut = lc_long.truncate(start=500, stop=1500, method='time')
In [30]:
lc_cut.time[0], lc_cut.time[-1]
Out[30]:
The time resolution (dt
) can also be changed to a larger value.
Note : While the new resolution need not be an integer multiple of the previous time resolution, be aware that if it is not, the last bin will be cut off by the fraction left over by the integer division.
In [31]:
lc_rebinned = lc_long.rebin(2)
In [32]:
print("Old time resolution = " + str(lc_long.dt))
print("Number of data points = " + str(lc_long.n))
print("New time resolution = " + str(lc_rebinned.dt))
print("Number of data points = " + str(lc_rebinned.n))
A lightcurve can be sorted using the sort
method.
In [33]:
new_lc_long = lc_long[:] # Copying into a new object
In [34]:
new_lc_long.sort(reverse=True)
In [35]:
new_lc_long.counts[0] == max(lc_long)
Out[35]:
Time array is also changed accordingly.
In [36]:
lc_long[new_lc_long.time[0]] == max(lc_long)
Out[36]:
A curve can be plotted with the plot
method.
In [37]:
lc.plot()
A plot can also be customized using several keyword arguments.
In [38]:
lc.plot(labels=('Time', "Counts"), # (xlabel, ylabel)
axis=(0, 1000, -50, 150), # (xmin, xmax, ymin, ymax)
title="Random generated lightcurve",
marker='c:') # c is for cyan and : is the marker style
The figure drawn can also be saved in a file using keywords arguments in the plot method itself.
In [39]:
lc.plot(marker = 'k', save=True, filename="lightcurve.png")
Note : See utils.savefig
function for more options on saving a file.
Stingray also has a sample Lightcurve
data which can be imported from within the library.
In [40]:
from stingray import sampledata
In [41]:
lc = sampledata.sample_data()
In [42]:
lc.plot()
In [ ]: